package com.github.linkoog.model_designer.ide.editer;

import com.github.linkoog.model_designer.ide.editer.common.DbType;
import com.github.linkoog.model_designer.ide.editer.common.DbTypeComboBoxCellRender;
import com.github.linkoog.model_designer.ide.editer.common.TableVo;
import com.github.linkoog.model_designer.utils.DDLSqlUtil;
import com.github.linkoog.model_designer.utils.FileTypeUtil;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.components.JBLoadingPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ItemEvent;


public class DDLSqlDialog extends DialogWrapper {

    private final Project project;
    private final TableVo tableVo;

    private final JBLoadingPanel loadingPanel = new JBLoadingPanel(new BorderLayout(), this::dispose);
    private JPanel myMainPanel;
    private JComboBox dbTypeComboBox;
    private JScrollPane sqlTextScrollPane;

    private Editor sqlTextEditor;



    public DDLSqlDialog(@NotNull Project project, @NotNull TableVo tableVo) {
        super(project);
        loadingPanel.setLoadingText("init...");
        loadingPanel.startLoading();

        this.project = project;
        this.tableVo = tableVo;
        myMainPanel.setMinimumSize(new Dimension(770, 550));
        loadingPanel.add(myMainPanel, BorderLayout.CENTER);

        dbTypeComboBox.setRenderer(new DbTypeComboBoxCellRender());
        for (DbType dbType : DbType.values()) {
            dbTypeComboBox.addItem(dbType);
        }

        dbTypeComboBox.addItemListener(event -> {
            if (event.getStateChange() == ItemEvent.SELECTED) {
                final Application application = ApplicationManager.getApplication();
                application.invokeLater(() -> {WriteCommandAction.runWriteCommandAction(project, () -> {
                    sqlTextEditor.getDocument().setText(DDLSqlUtil.createDDLSql(getDbType(), tableVo));
                });});
            }
        });

        // 设置 sql text editor
        EditorFactory editorFactory = EditorFactory.getInstance();
        Document document = editorFactory.createDocument(DDLSqlUtil.createDDLSql(getDbType(), tableVo));
        sqlTextEditor = editorFactory.createEditor(document, project, FileTypeUtil.getFileType("SQL"), false);
        sqlTextScrollPane.setViewportView(sqlTextEditor.getComponent());

        init();
        loadingPanel.stopLoading();
    }

    @Override
    protected void doOKAction() {
        final String ddl = getCreateTableSql();
        CopyPasteManager.getInstance().setContents(new StringSelection(ddl));
        super.doOKAction();
    }

    public String getCreateTableSql() {
        return sqlTextEditor.getDocument().getText();
    }

    private DbType getDbType(){
        return (DbType) dbTypeComboBox.getSelectedItem();
    }

    public JPanel getMyMainPanel() {
        return loadingPanel;
    }

    @Override
    public JPanel createCenterPanel() {
        return loadingPanel;
    }

    private void createUIComponents() {
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new GridLayoutManager(2, 2, new Insets(0, 0, 0, 0), -1, -1));
        final JLabel label1 = new JLabel();
        label1.setText("数据库");
        myMainPanel.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        dbTypeComboBox = new JComboBox();
        myMainPanel.add(dbTypeComboBox, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        sqlTextScrollPane = new JScrollPane();
        myMainPanel.add(sqlTextScrollPane, new GridConstraints(1, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }


}
